package cn.sumpu.app.coolsk.dao.impl;

import java.util.*;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.*;
import cn.sumpu.app.coolsk.dao.*;
import cn.sumpu.app.coolsk.domain.*;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class ChannelDaoImpl extends SqlSessionDaoSupport implements ChannelDao {
	private final static Logger logger = LoggerFactory
			.getLogger(ChannelDaoImpl.class);

	// 添加频道
	@Override
	public void addChannel(Channel channel) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.coolsk.persistence.ChannelMapper.addChannel",
					channel);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("addChannel resource failure.", e);
			}
			throw new AppBusinessException("addChannel resource failure.", e);
		}
	}

	//更新频道
	@Override
	public void updateChannel(Channel channel) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.coolsk.persistence.ChannelMapper.updateChannel",
					channel);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateChannel resource failure.", e);
			}
			throw new AppBusinessException("updateChannel resource failure.", e);
		}
	}
	//返回频道树结构
	@SuppressWarnings("unchecked")
	@Override
	public List<Channel> getChannelAll(String parent)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ChannelMapper.getChannelAll",
					parent);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getChannelAll resource failure.", e);
			}
			throw new AppBusinessException("getChannelAll resource failure.", e);
		}
	}
	//查询频道树(排除栏目)
	@SuppressWarnings("unchecked")
	@Override
	public List<Channel> getOnlyChannelAll(String parent)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ChannelMapper.getOnlyChannelAll",
					parent);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getOnlyChannelAll resource failure.", e);
			}
			throw new AppBusinessException("getOnlyChannelAll resource failure.", e);
		}
	}
	//查询频道子节点
	@SuppressWarnings("unchecked")
	@Override
	public List<Channel> getChildChannel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession().selectList(
					"cn.sumpu.app.coolsk.persistence.ChannelMapper.getChildChannel",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getChildChannel resource failure.", e);
			}
			throw new AppBusinessException("getChildChannel resource failure.", e);
		}
	}
	//根据ID查询频道
	@Override
	public Channel getChannelById(String id) throws AppBusinessException {
		try {
			return (Channel) getSqlSession().selectOne(
					"cn.sumpu.app.coolsk.persistence.ChannelMapper.getChannelById",
					id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getChannelById resource failure.", e);
			}
			throw new AppBusinessException("getChannelById resource failure.", e);
		}
	}
	//根据mark查询频道
	@Override
	public Channel getChannelByMark(String mark) throws AppBusinessException {
		try {
			return (Channel) getSqlSession().selectOne(
					"cn.sumpu.app.coolsk.persistence.ChannelMapper.getChannelByMark",
					mark);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getChannelByMark resource failure.", e);
			}
			throw new AppBusinessException("getChannelByMark resource failure.", e);
		}
	}

}
